home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / c_plasma / plasma2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-20  |  8.5 KB  |  390 lines

  1. /*
  2.     Plasma by Jan M¢ller & Erik Hansen.
  3.  
  4.        It was compiled in Borland c++, in ANSI mode, using the HUGE memory model.
  5.     Feel free to use it as you desire, you may even name it after your
  6.     grandmother. We don't care.
  7.        The reason why we made it? Well... First of all we wanted to make some
  8.     plasma, just for fun. But when we had finished it turned out to be much
  9.     faster than the plasma we have seen in intros, demos etc...
  10.     Normally the color-cells are 2x4 4x4 or even larger, the 2x4 cell-plasma
  11.     was awfully slow, bot ours ain't, even though it is 2x2 cells.
  12.        Well how can that be???                        ^^^^^^^^^
  13.     Our secret lies in the plasma-calculation!
  14.     (I assume you have guessed that    part already)
  15.     We simply calculate as much as possible before we start showing the goddies.
  16.     The table 'Tab1' is a simple table (320x200 yields 64k) with the distance
  17.     from (x,y) to the center (rounded off to char by simple overflow). The second
  18.     table 'Tab2' is similar to 'Tab1', except we molested it with sine.
  19.     In the mainloop we calculate a body (160x100) by accessing the two tables
  20.     with different pairs of (x,y) and add them.
  21.     (see for yourself in 'CalculateBody')
  22.     And KaPoW. We have the fastest plasma...
  23.     (i.e. the fastest we have ever seen.(on a 486)) If I am not correct then please notify me.
  24.  
  25.     If you have any questions, comments or whips, then we would be happy to answer.
  26.  
  27.     Contact us trough E-Mail:
  28.  
  29.     fwiffo@daimi.aau.dk
  30.  
  31.             or
  32.  
  33.     martino@daimi.aau.dk
  34.  
  35.     (If you can optimize it (e.g. write it in asm) we would be very interested
  36.      to see the results!)
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <conio.h>
  42. #include <math.h>
  43. #include <fcntl.h>
  44. #include <sys\stat.h>
  45. #include <io.h>
  46. #include <dos.h>
  47. #define uchar unsigned char
  48. #define ulong unsigned long
  49. #define uint unsigned int
  50. #define PEL_write 0x3c8
  51. #define PEL_read 0x3c7
  52. #define PEL_data 0x3c9
  53. #define box_w 160
  54. #define box_h 100
  55. #define pi 3.1415926L
  56. //-------------------------------------------------
  57. uchar body[box_w*box_h];    // buffer for the bitmap body
  58. uchar colors[256];            // color table
  59. uchar tab1[64000];            // table one for thr plasma
  60. uchar tab2[64000];            // table two for the plasma
  61. uchar picture[64004];        // buffer for the picture
  62. //-------------------------------------------------
  63.  
  64. //**********************************************************
  65. //*******************  ASM SOURCES  ************************
  66. //**********************************************************
  67.  
  68. void OnVideo(void)
  69. {
  70.     asm mov bl,0x36
  71.     asm mov ax,0x1200
  72.     asm int 0x10
  73. }
  74. void OffVideo(void)
  75. {
  76.     asm mov bl,0x36
  77.     asm mov ax,0x1201
  78.     asm int 0x10
  79. }
  80. void SetMode(char mode)
  81. {
  82.     asm mov ah,0x00
  83.     asm mov al,mode
  84.     asm int 0x10
  85. }
  86. char GetMode()
  87. {
  88.     char mode;
  89.     asm mov ah,0x0f
  90.     asm int 0x10
  91.     asm mov mode,al
  92.     return (mode);
  93. }
  94. void WaitRast()
  95. {
  96.     asm mov dx,0x3da
  97. bra1:
  98.     asm in  al,dx
  99.     asm and al,8
  100.     asm jnz bra1
  101. bra2:
  102.     asm in  al,dx
  103.     asm and al,8
  104.     asm jz bra2
  105. }
  106. void SetBank(unsigned char wbank, unsigned char rbank)
  107. {
  108.     unsigned char n=rbank<<4|wbank;
  109.     asm mov al,n
  110.     asm mov dx,0x3cd    // Point to memory segment register
  111.     asm out dx,al        // Set it.
  112. }
  113. void SetDisplayStart(int adsr)
  114. {
  115.     // This part uses standard VGA DisplayStart registers
  116.     asm mov bx,adsr        // Load start adress
  117.     asm mov dx,0x3d0    // color register (mono = 0x3b0
  118.     asm add dx,4
  119.     asm mov al,0x0d
  120.     asm out dx,al
  121.     asm inc dx
  122.     asm mov al,bl
  123.     asm out dx,al
  124.     asm dec dx
  125.     asm mov al,0x0c
  126.     asm out dx,al
  127.     asm inc dx
  128.     asm mov al,bh
  129.     asm out dx,al        // Listing 14.32
  130. }
  131.  
  132. void DubImage(int off, unsigned char buf[], int nbyte, int nrow)
  133. {
  134.     asm push es
  135.     asm mov ax,0xa000
  136.     asm mov es,ax
  137.     asm mov di,off
  138.     asm lds si,buf
  139.     asm xor dx,dx
  140.     asm mov bx,320
  141.     asm sub bx,nbyte
  142.     asm sub bx,nbyte
  143.     asm mov cx,nrow
  144.     asm xor ch,ch
  145.     asm mov ah,[bp+10]
  146.  
  147. loop1:
  148.     asm mov dx,nbyte
  149. loop2:
  150.     asm mov al,[si]
  151.     asm mov ah,al
  152.     asm mov es:[di],ax
  153.     asm inc di
  154.     asm inc di
  155.     asm inc si
  156.     asm dec dx
  157.     asm jg loop2
  158.     asm add di,bx
  159.     asm sub si,nbyte
  160.     asm mov dx,nbyte
  161. loop3:
  162.     asm mov al,[si]
  163.     asm mov ah,al
  164.     asm mov es:[di],ax
  165.     asm inc di
  166.     asm inc di
  167.     asm inc si
  168.     asm dec dx
  169.     asm jg loop3
  170.     asm add di,bx
  171.     asm loop loop1
  172.     asm pop es
  173. }
  174. void SetPal(uchar col, uchar red, uchar gre, uchar blu)
  175. {
  176.     asm mov al,col        // load palette
  177.     asm mov dx,PEL_write    // Ignite write mode
  178.     asm out dx,al        // store it
  179.     asm mov dx,PEL_data    // Initialize color data
  180.     asm mov al,red        // red color
  181.     asm out dx,al        // store it
  182.     asm mov al,gre        // green color
  183.     asm out dx,al        // store it
  184.     asm mov al,blu        // blue color
  185.     asm out dx,al        // and store it
  186. }
  187. void LoadR13(char *src)
  188. {
  189.     int shandle,dhandle;
  190.     ulong bytes;
  191.     unsigned char head[48];
  192.     unsigned char *srcbuf;
  193.     ulong size,size2;
  194.     ulong dummy,x,y;
  195.  
  196.     if ((shandle = open(src, O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
  197.     {    printf("Error Opening Src-File\n"); exit(1);   }
  198.     if ((bytes = read(shandle, head, 48)) == -1)
  199.     {   printf("Source Read Failed.\n"); exit(1);   }
  200.     close(shandle);
  201.     x = (int)(head[0])+((int)(head[1])<<8);
  202.     y =    (int)(head[2])+((int)(head[3])<<8);
  203.     size=x*y+4;
  204.     if ((shandle=open(src, O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
  205.     { printf("Error Opening Src-File\n");exit(1);}
  206.     if ((bytes = read(shandle, picture, size)) == -1)
  207.     { printf("Source Picture read Failed.\n");exit(1); }
  208. }
  209.  
  210. void CalcTab1()   // calculate table 1 for plasma
  211. {
  212.     long i=0,j=0;
  213.     while(i<box_h*2)
  214.     {
  215.         j=0;
  216.         while(j<box_w*2)
  217.         {
  218.             tab1[(i*box_w*2)+j]=(uchar) ( sqrt((box_h-i)*(box_h-i)+(box_w-j)*(box_w-j)) *5 );
  219.             j++;
  220.         }
  221.         i++;
  222.     }
  223. }
  224. void CalcTab2()   // calculate table 2 for plasma
  225. {
  226.     long i=0,j=0;
  227.     float temp;
  228.     while(i<box_h*2)
  229.     {
  230.         j=0;
  231.         while(j<box_w*2)
  232.         {
  233.             temp=sqrt(16.0+(box_h-i)*(box_h-i)+(box_w-j)*(box_w-j))-4;
  234.             tab2[(i*box_w*2)+j]=(sin(temp/9.5)+1)*90;
  235. //            tab2[(i*box_w*2)+j]=(sin(sqrt((box_h-i)*(box_h-i)+(box_w-j)*(box_w-j))/9.5)+1)*90;
  236.             j++;
  237.         }
  238.         i++;
  239.     }
  240. }
  241. void CalculateColors()
  242. {
  243.     static double r=1.0/6.0*pi,g=3.0/6.0*pi,b=5.0/6.0*pi;
  244.     int i=0;
  245.     double u,v;
  246.     while(i<256)
  247.     {
  248.         u=2*pi/256*i;
  249. //#define mycol(u,a) (max(0.0,cos((u)+(a))))*63 //31
  250. #define mycol(u,a) (cos((u)+(a))+1)*31
  251.         SetPal(i,mycol(u,r),mycol(u,g),mycol(u,b));
  252.         i++;
  253.     }
  254.     r+=0.05;
  255.     g-=0.05;
  256.     b+=0.1;
  257. }
  258. void CalculateBody(long x1,long y1,long x2,long y2,long x3,long y3,long x4,long y4,long roll,long picx,long picy)
  259. {
  260.     long i=0,j=0,k=0;
  261.     char a;
  262.     while(i<box_h)
  263.     {
  264.         j=0;
  265.         k+=box_w;
  266.         while(j<box_w)
  267.         {
  268.                                 // this is the heart of the plasma
  269.             body[k+j]=
  270.                     tab1[320*(i+y1)+j+x1]+roll+
  271.                     tab2[320*(i+y2)+j+x2]+
  272.                     tab2[320*(i+y3)+j+x3]+
  273.                     tab2[320*(i+y4)+j+x4]+
  274.                     picture[320*(i+picy)+j+picx+4]
  275.                     ;
  276.             j++;
  277.         }
  278.         i++;
  279.     }
  280. }
  281. void UpdatePic(int *picx,int *picy,int *picc)
  282. {
  283.     if(*picc<100)
  284.     {
  285.         *picc+=1;
  286.         return;
  287.     }
  288.     if(*picc<150)
  289.     {
  290.         *picc+=1;
  291.         *picy+=2;
  292.         return;
  293.     }
  294.     if(*picc<250)
  295.     {
  296.         *picc+=1;
  297.         return;
  298.     }
  299.     if(*picc<330)
  300.     {
  301.         *picc+=1;
  302.         *picx+=2;
  303.         return;
  304.     }
  305.     if(*picc<430)
  306.     {
  307.         *picc+=1;
  308.         return;
  309.     }
  310.     if(*picc<480)
  311.     {
  312.         *picc+=1;
  313.         *picy-=2;
  314.         return;
  315.     }
  316.     if(*picc<580)
  317.     {
  318.         *picc+=1;
  319.         return;
  320.     }
  321.     if(*picc<660)
  322.     {
  323.         *picc+=1;
  324.         *picx-=2;
  325.         return;
  326.     }
  327.     *picc=0;
  328. }
  329. void DisplayObject(int x,int y)
  330. {
  331.     DubImage(x+y*320,&body[0],box_w,box_h);
  332. }
  333. int main(void)
  334. {
  335.     float circle1=0,circle2=0,circle3=0,circle4=0,circle5=0,circle6=0,circle7=0,circle8=0;
  336.     int x1,y1,x2,y2,x3,y3,x4,y4,roll1=0;
  337.     int bank=0,i=0;
  338.     int picx=0,picy=0,picc=0;
  339.     char oldmode;
  340. //    char *picture;
  341.     LoadR13("text.r13");
  342.     oldmode=GetMode();
  343.     printf("\nSorry no music...\nPlease stand by, as CPU is performing heavy calculations.\n");
  344.     CalcTab1();
  345.     CalcTab2();
  346.     OnVideo();
  347.     SetMode(19);
  348.     CalculateColors();
  349.     while(!kbhit())
  350.     {
  351.         i++;
  352.         circle1+=0.085/6;
  353.         circle2-=0.1/6;
  354.         circle3+=.3/6;
  355.         circle4-=.2/6;
  356.         circle5+=.4/6;
  357.         circle6-=.15/6;
  358.         circle7+=.35/6;
  359.         circle8-=.05/6;
  360.         x2=(box_w/2)+(box_w/2)*sin(circle1);
  361.         y2=(box_h/2)+(box_h/2)*cos(circle2);
  362.         x1=(box_w/2)+(box_w/2)*cos(circle3);
  363.         y1=(box_h/2)+(box_h/2)*sin(circle4);
  364.         x3=(box_w/2)+(box_w/2)*cos(circle5);
  365.         y3=(box_h/2)+(box_h/2)*sin(circle6);
  366.         x4=(box_w/2)+(box_w/2)*cos(circle7);
  367.         y4=(box_h/2)+(box_h/2)*sin(circle8);
  368.         CalculateBody(x1,y1,x2,y2,x3,y3,x4,y4,roll1+=5,picx,picy);
  369.         UpdatePic(&picx,&picy,&picc);
  370.         DisplayObject((320-box_w*2)/2,0);
  371. //        WaitRast();
  372.         if (bank==0)
  373.         {
  374.             bank=1;
  375.             SetBank(0,0);
  376.             SetDisplayStart(16384);
  377.             CalculateColors();
  378.         }
  379.         else
  380.         {
  381.             bank=0;
  382.             SetBank(1,1);
  383.             SetDisplayStart(0);
  384.         }
  385.     }
  386.     OffVideo();
  387.     SetMode(oldmode);
  388.     return 0;
  389. }
  390.